<?php

namespace app\health\service;

use Exception;

/**
 * 微信支付接口服务
 * @method \WePay\Order WePayOrder() static 微信商户订单
 */
class WePayService
{

    /**
     * 静态初始化对象
     * @param string $name
     * @param array $arguments
     * @return mixed
     */
    public static function __callStatic($name, $arguments)
    {
        [$type, $base, $class] = static::parseName($name);
        if ("{$type}{$base}" !== $name) {
            throw new Exception("抱歉，实例 {$name} 不符合规则！");
        }
        if (class_exists($class)) {
            return new $class(static::getConfig());
        } else {
            throw new Exception("抱歉，接口模式无法实例 {$class} 对象！");
        }
    }

    /**
     * 解析调用对象名称
     *
     * @param [type] $name
     * @return array
     */
    private static function parseName($name)
    {
        if (strpos($name, 'WePay') === 0) {
            [, $base] = explode('WePay', $name);
            return ['WePay', $base, "\\WePay\\{$base}"];
        }
        return ['-', '-', $name];
    }

    /**
     * 获取支付配置参数
     * @return array
     */
    public static function getConfig()
    {
        return ConfigService::getWechatPayConfig();
    }
}
